<?php

namespace Apps\AppApi\Method\Order;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Enum\OrderState;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\UserOrder;

/**
 * 设置订单状态
 * @package Apps\AppApi\Method\Order
 */
class SetOrderState extends BaseMethod
{
    public function run()
    {
        /**
         * 验证用户权限
         */
        if(false == $this->verifyUserAuth()){
            return false;
        }
        $id = intval($this->getDataItem('id', 0));
        $state = $this->getDataItem('state', null);
        if($id <= 0 || empty($state)) {
            return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR);
        }
        $stateValues = ['cancel', 'delete'];
        if(!in_array($state, $stateValues)) {
            return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '状态值错误.');
        }
        $stateValue = 0;
        switch($state) {
            case $stateValues[0]:
                $stateValue = OrderState::Cancel;
                break;
            case $stateValues[1]:
                $stateValue = OrderState::Delete;
                break;
        }
        UserOrder::SetOrderState($this->getUserAuth()->userId, $id, $stateValue);
        $this->setResult([
            'success' => 1,
        ]);
    }
}